package com.wzj.yygh.order.controller.front;

import com.wzj.yygh.common.result.R;
import com.wzj.yygh.common.utils.AuthContextHolder;
import com.wzj.yygh.model.order.OrderInfo;
import com.wzj.yygh.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "订单表")
@RestController
@RequestMapping("front/order/orderInfo")
public class FrontOrderInfoController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("创建订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public R submitOrder(@PathVariable String scheduleId, @PathVariable Long patientId, HttpServletRequest request) {
        //校验用户是否登录
        AuthContextHolder.checkAuth(request);
        //下订单
        Long orderId = orderService.submitOrder(scheduleId,patientId);
        return R.ok().data("orderId",orderId);
    }

    @ApiOperation("根据订单id查询订单详情")
    @GetMapping("auth/getOrder/{orderId}")
    public R getOrder(@PathVariable Long orderId,HttpServletRequest request) {
        //安全校验
        AuthContextHolder.checkAuth(request);
        // TODO 带上用户ID进行优化查询
        OrderInfo orderInfo = orderService.getOrderInfoByOrderId(orderId);
        return R.ok().data("orderInfo",orderInfo);
    }
    @ApiOperation("订单列表")
    @GetMapping("auth/list")
    public R list(HttpServletRequest request) {
        //安全校验
        Long userId = AuthContextHolder.getUserId(request);
        List<OrderInfo> orderInfoList = orderService.selectList(userId);
        return R.ok().data("orderInfoList",orderInfoList);
    }

    @ApiOperation("取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public R cancelOrder(@PathVariable("orderId") Long orderId,HttpServletRequest request) {
        //安全校验
        Long userId = AuthContextHolder.getUserId(request);
        orderService.cancelOrder(orderId);
        return R.ok().message("预约已取消");
    }


}
